import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;

public class JdkZipCompress {
	private static final int bufferSize = 4096;
	
	public static void doCompress(List<File> srcFiles, File compressFile) throws IOException {
		// TODO Auto-generated method stub
		ZipOutputStream os = null;
		InputStream is = null;
		try {
			//is = new BufferedInputStream(new FileInputStream(srcFiles), bufferSize);
			os = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compressFile),bufferSize));
			for(File f :srcFiles) {
				is = new BufferedInputStream(new FileInputStream(f), bufferSize);
				os.putNextEntry(new ZipEntry(f.getName()));
				IOUtils.copy(is, os);
				os.closeEntry();
				is.close();
				//IOUtils.closeQuietly(is);
			}
			//IOUtils.copy(is, os);
			//os.closeEntry();
		} finally {
			//IOUtils.closeQuietly(is);
			IOUtils.closeQuietly(os);
		}
	}
	
	public static void doDecompress(File compressFile, File destDir) throws IOException {
		ZipInputStream is = null;
		try {
			is = new ZipInputStream(new BufferedInputStream(new FileInputStream(compressFile), bufferSize));
			ZipEntry entry = null;
			while ((entry = is.getNextEntry()) != null) {
				if (entry.isDirectory()) {
					File directory = new File(destDir, entry.getName());
					directory.mkdirs();
					is.closeEntry();
				} else {
					OutputStream os = null;
					try {
						os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())),bufferSize);
						IOUtils.copy(is, os);
					} finally {
						IOUtils.closeQuietly(os);
					}
					is.closeEntry();
				}
			}
			
		} finally {
			IOUtils.closeQuietly(is);			
		}
	}
}

